package cn.aishua.ui.myKaka;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.aishua.R;
import cn.aishua.network.HttpRequestUtil;
import cn.aishua.ui.MainActivityGroup;
import cn.aishua.ui.myKaka.task.DataExchangeAsyncTask;
import cn.aishua.util.FormatConverter;

public class CreditExchangeNextActivity
    extends Activity
{
    private TextView tvAvailableCredits;
    private TextView tvUnavailableCredits;

    private TextView tvCredits;
    private TextView tvAmount;
    private TextView tvMobileNumber;
    private EditText etAuthCode;
    private Button btnGetAuthCode;
    private MyTimer timer = new MyTimer( 60000, 1000 );

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.my_kaka_credit_exchange_next );
        initiate();
    }

    private void initiate()
    {
        tvAvailableCredits = (TextView) findViewById( R.id.my_kaka_credit_exchange_next_tv_available_credits );
        tvUnavailableCredits = (TextView) findViewById( R.id.my_kaka_credit_exchange_next_tv_unavailable_credits );

        tvCredits = (TextView) findViewById( R.id.my_kaka_credit_exchange_next_credits );
        tvAmount = (TextView) findViewById( R.id.my_kaka_credit_exchange_next_amount );
        tvMobileNumber = (TextView) findViewById( R.id.my_kaka_credit_exchange_next_mobile_number );
        etAuthCode = (EditText) findViewById( R.id.my_kaka_credit_exchange_next_auth_code );
        btnGetAuthCode = (Button) findViewById( R.id.my_kaka_credit_exchange_next_btn_get_auth_code );

        Intent intent = getIntent();
        String credits = intent.getStringExtra( CreditExchangeActivity.EXTRA_MSG_EXCHANGE_CREDITS );
        String amount = intent.getStringExtra( CreditExchangeActivity.EXTRA_MSG_AMCOUNTS );
        String mobileNumber = getSharedPreferences( "userInfo", Context.MODE_PRIVATE ).getString(
            MemberCenterActivity.SHARED_PREF_STR_MOBILE_NUMBER, "" );

        tvAvailableCredits.setText( intent.getStringExtra( CreditExchangeActivity.EXTRA_MSG_AVAILABLE_CREDITS ) );
        tvUnavailableCredits.setText( intent.getStringExtra( CreditExchangeActivity.EXTRA_MSG_UNAVAILABLE_CREDITS ) );

        tvCredits.setText( credits );
        tvAmount.setText( amount );
        tvMobileNumber.setText( mobileNumber );

    }

    public void submit( View view )
    {
        String captcha = etAuthCode.getText().toString();
        String credit = tvCredits.getText().toString();

        if( !FormatConverter.isNotEmptyString( captcha ) )
        {
            Toast.makeText( this, "请输入验证码", Toast.LENGTH_SHORT ).show();
            return;
        }

        new DataExchangeAsyncTask( this, "提交请求" )
        {

            @Override
            protected HttpResponse sendRequest( String... params )
            {
                return HttpRequestUtil.getInstance().cashCredit( params );
            }

            @Override
            protected void handleSuccResponse( String strJson )
            {
                Toast.makeText( CreditExchangeNextActivity.this, "兑现申请已提交,爱刷将在3-5个工作日内将对现金额转至您的银行卡账户中",
                    Toast.LENGTH_SHORT ).show();
                Intent intent = new Intent( CreditExchangeNextActivity.this, MainActivityGroup.class );
                intent.putExtra( MainActivityGroup.TAB, MainActivityGroup.TAG_USER_INFO );
                startActivity( intent );
                finish();
            }
        }.execute( credit, captcha );

    }

    public void back( View view )
    {
        onBackPressed();
    }

    public void getAuthCode( View view )
    {
        new DataExchangeAsyncTask( this, "获取短信验证码" )
        {

            @Override
            protected HttpResponse sendRequest( String... params )
            {
                return HttpRequestUtil.getInstance().sendCashingCaptcha();
            }

            @Override
            protected void handleSuccResponse( String strJson )
            {
                Toast.makeText( CreditExchangeNextActivity.this, "已发送验证码短信到" + tvMobileNumber.getText() + "，请查收",
                    Toast.LENGTH_SHORT ).show();
                timer.start();
            }
        }.execute();
    }

    class MyTimer
        extends CountDownTimer
    {
        public MyTimer(long millisInFuture, long countDownInterval)
        {
            super( millisInFuture, countDownInterval );
        }

        @Override
        public void onTick( long millisUntilFinished )
        {
            btnGetAuthCode.setText( "重发验证码(" + millisUntilFinished / 1000 + ")" );
            btnGetAuthCode.setClickable( false );
        }

        @Override
        public void onFinish()
        {
            btnGetAuthCode.setText( "获取短信验证码" );
            btnGetAuthCode.setClickable( true );
        }

    }

    @Override
    protected void onPause()
    {
        super.onPause();
        timer.cancel();
    }

}
